Const ForReading   = 1
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject ("WScript.shell")
Set objTomcat = objFSO.OpenTextFile("Config\Tomcat_config.txt", ForReading)
Set objIBoss = objFSO.OpenTextFile("Config\iBoss_config.txt", ForReading)

Dim LogName, myDateString , tomcatRootLocation, ibossLocation
tomcatCatAuto = true
myDateString = FormatDateTime(Date(), 1)
LogName = "Log\" + "LogFile_" & myDateString + ".txt" 
Set objLog = objFSO.OpenTextFile( LogName, ForAppending, true)

objLog.WriteLine "//============================== Start =================================//"
'Get Tomcat Path and Version
tomcatVersion = objTomcat.ReadLine
tomcatVersion = Trim(replace(replace(tomcatVersion,"tomcat_version",""),"=",""))
tomcatRootLocation = objTomcat.ReadLine
tomcatRootLocation = Trim(replace(replace(tomcatRootLocation,"tomcat_path",""),"=",""))
tomcatAuto = objTomcat.ReadLine
tomcatAuto = Trim(replace(replace(tomcatAuto,"tomcat_auto_restart",""),"=",""))
objLog.WriteLine ("["&Time&"]: ") + "Found Tomcat Location: "+tomcatRootLocation
objLog.WriteLine ("["&Time&"]: ") + "Found Tomcat Verion: "+tomcatVersion
objLog.WriteLine ("["&Time&"]: ") + "Tomcat Auto Run Started"

Comp = StrComp(tomcatAuto, "on", vbTextCompare)
If Comp = 0 Then
	tomcatCatAuto = true
Else 
	tomcatCatAuto = false
End If

Function OutLog(log)
	objLog.WriteLine ("["&Time&"]: ") + log
End Function

'Tomcat Control Funtion,  Action = start or close
Function tomcatControl(Action)
	If tomcatCatAuto Then
		Dim LogStart, LogClose, TomcatOutput
		If StrComp(Action, "start", vbTextCompare) Then
			LogStart = "Starting"
			LogClose = "Start"
		Else 
			LogStart = "Closing"
			LogClose = "Close"
		End If
		objLog.WriteLine ("["&Time&"]: ") + LogStart +" tomcat "
		commandKey = "cmd /K net "+Action+ " "+ tomcatVersion + "& exit"
		TomcatOutput = objShell.run(commandKey, true)
		objLog.WriteLine ("["&Time&"]: ") + LogClose +" tomcat success --"&TomcatOutput
		tomcatControl = TomcatOutput
	End If
End Function

'Stop Tomcat 
tomcatControl "close"

If objFSO.FolderExists(tomcatRootLocation) Then
	If objFSO.FolderExists(tomcatRootLocation + "\webapps\iBoss2JWS") Then 
		objLog.WriteLine ("["&Time&"]: ") + "Removing JWS Folder in Tomcat"
		Set origionalJwsFolderObj = objFSO.GetFolder(tomcatRootLocation + "\webapps\iBoss2JWS")
		origionalJwsFolderObj.Delete True
		objLog.WriteLine ("["&Time&"]: ") + "Remove Complete"
	Else
		objLog.WriteLine ("["&Time&"]: ") + "JWS Folder Not Exist in Tomcat Folder"
	End If
	If objFSO.FolderExists(tomcatRootLocation + "\webapps\iBoss2RS") Then 
		objLog.WriteLine ("["&Time&"]: ")+ "Removing RS Folder in Tomcat"
		Set origionalRSFolderObj = objFSO.GetFolder(tomcatRootLocation + "\webapps\iBoss2RS")
		origionalRSFolderObj.Delete True
		objLog.WriteLine ("["&Time&"]: ")+ "Remove Complete"
	Else
		objLog.WriteLine ("["&Time&"]: ")+ "RS Folder Not Exist in Tomcat Folder"
	End If
	If objFSO.FileExists(tomcatRootLocation + "\webapps\iBoss2JWS.war") Then 
		objLog.WriteLine ("["&Time&"]: ")+ "Removing iBoss2JWS.war in Tomcat"
		Set origionalJwsWarObj = objFSO.GetFile(tomcatRootLocation + "\webapps\iBoss2JWS.war")
		origionalJwsWarObj.Delete
		objLog.WriteLine ("["&Time&"]: ")+ "Remove Complete"
	Else
		objLog.WriteLine ("["&Time&"]: ")+ "iBoss2JWS.war Not Exist in Tomcat Folder"
	End If
	If objFSO.FileExists(tomcatRootLocation + "\webapps\iBoss2RS.war") Then 
		objLog.WriteLine ("["&Time&"]: ")+ "Removing iBoss2RS.war in Tomcat"
		Set origionalRSWarObj = objFSO.GetFile(tomcatRootLocation + "\webapps\iBoss2RS.war")
		origionalRSWarObj.Delete
		objLog.WriteLine ("["&Time&"]: ")+ "Remove Complete"
	Else
		objLog.WriteLine ("["&Time&"]: ")+ "iBoss2RS.war Not Exist in Tomcat Folder"
	End If
	
	'get iboss2 Path and Version
	ibossVersion = objIBoss.ReadLine
	ibossVersion = Trim(replace(replace(ibossVersion,"iboss_version",""),"=",""))
	ibossLocation = objIBoss.ReadLine
	ibossLocation = Trim(replace(replace(ibossLocation,"iboss_path",""),"=",""))
	
	objLog.WriteLine ("["&Time&"]: ")+ "Found iBoss2 Location: " + ibossLocation
	If objFSO.FolderExists(ibossLocation+"\iBoss2JWS") Then
		If objFSO.FolderExists(ibossLocation+"\iBoss2JWS\target") Then 
			If objFSO.FileExists(ibossLocation+"\iBoss2JWS\target\iBoss2JWS.war") Then
				objLog.WriteLine ("["&Time&"]: ")+ "Found iBoss2JWS.war, Copying to Tomcat Folder"
				Set objJwsFile = objFSO.GetFile(ibossLocation+"\iBoss2JWS\target\iBoss2JWS.war")
				objJwsFile.Copy tomcatRootLocation + "\webapps\"
				objLog.WriteLine ("["&Time&"]: ")+ "iBoss2JWS.war copy completed"
			Else
				Wscript.Echo "iBoss2JWS Folder Missing iBoss2JWS.war, Please recompile again"
			End IF
		Else
			Wscript.Echo "iBoss2JWS Folder Missing Target Folder, Please recompile again"
		End If
	Else
		Wscript.Echo "iBoss2JWS Folder Not Exist in iBoss Folder"
	End If
	
	If objFSO.FolderExists(ibossLocation+"\iBoss2RS") Then
		If objFSO.FolderExists(ibossLocation+"\iBoss2RS\target") Then 
			If objFSO.FileExists(ibossLocation+"\iBoss2RS\target\iBoss2RS.war") Then
				objLog.WriteLine ("["&Time&"]: ")+ "Found iBoss2RS.war, Copying to Tomcat Folder"
				Set objRSFile = objFSO.GetFile(ibossLocation+"\iBoss2RS\target\iBoss2RS.war")
				objRSFile.Copy tomcatRootLocation + "\webapps\"
				objLog.WriteLine ("["&Time&"]: ")+ "iBoss2RS.war copy completed"
			Else
				Wscript.Echo "iBoss2RS Folder Missing iBoss2RS.war, Please recompile again"
			End IF
		Else
			Wscript.Echo "iBoss2RS Folder Missing Target Folder, Please recompile again"
		End If
	Else
		Wscript.Echo "iBoss2JWS Folder Not Exist in iBoss Folder"
	End If
Else
	Wscript.Echo "Tomcat Location Error"
End If


'Start Tomcat 
tomcatControl "start"

objLog.WriteLine "//=============================== End ==================================//"
Wscript.Echo "Refresh Complete"
objTomcat.Close
objIBoss.Close